home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 5 / QRZ Ham Radio Callsign Database - Volume 5.iso / unix / src / nucall.c < prev    next >
C/C++ Source or Header  |  1993-11-01  |  6KB  |  340 lines

  1. /*
  2. **
  3. **    nucall.c
  4. **
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <sys/mman.h>
  11. #include <time.h>
  12. #include <fcntl.h>
  13. #include <string.h>
  14. #include <signal.h>
  15. #include <termio.h>
  16.  
  17.  
  18. int done();
  19. int quit();
  20.  
  21. int mon_handle;
  22. long    start;
  23. char    name[512];
  24.  
  25. main(argc,argv)
  26. int argc;
  27. char *argv[];
  28. {    
  29.     struct stat st;
  30.     char    call[8];
  31.     char    *line;
  32.     char    *p;
  33.     long    idsize;
  34.     char    c[2];
  35.     char    buf[512];
  36.     char    look[256];
  37.     char    pbuf[2048];
  38.     int    lookt;
  39.     int    counter;
  40.     int    found;
  41.     int    pause = 0;
  42.     time_t    now;
  43.     FILE *cp;
  44.     FILE *fp;
  45.     FILE *wfp;
  46.  
  47.     time(&start);
  48.     setmodes();
  49.     if ((cp=fopen("93061.cbk","r")) == NULL)
  50.     {
  51.         printf("Error opening callbook.db\n\r");
  52.         quit();
  53.     }
  54.  
  55.     signal(SIGALRM,(void *)done);
  56.     alarm(900);
  57.     signal(SIGHUP,(void *)quit);
  58.     signal(SIGINT,(void *)quit);
  59.     signal(SIGQUIT,(void *)quit);
  60.  
  61.     monitor();
  62.     mprintf("\n\rWelcome to the AA7BQ Callsign Server\n\r");
  63.  
  64.     if ((wfp=fopen("welcome.callbook","r")) != NULL)
  65.     {
  66.         while (!feof(wfp))
  67.         {    fgets(buf,sizeof(buf),wfp);
  68.             mprintf(buf);
  69.         }
  70.         fclose(wfp);
  71.     }
  72.  
  73.     memset(name,0,sizeof(name));
  74.     while(!strlen(name))
  75.     {
  76.         mprintf("\n\rPlease enter your name -> ");
  77.         strin(name,0);
  78.         counter++;
  79.             if (counter == 5)
  80.                 quit();
  81.     }
  82.     mprintf("\n\r\n\rHello %s, you have 15 minutes to use the database.\n\r",
  83.     name);
  84.  
  85.     time(&now);
  86.     if (fp=fopen("/tmp/bbs.user","w"))
  87.     {
  88.         strftime(buf,128,"%T",localtime(&now));
  89.         fprintf(fp,"%s (Callbook) on at %s",name,buf);
  90.         fclose(fp);
  91.     }
  92.  
  93.     mprintf("\n\rThe data presented on this system was copied\n\r");
  94.     mprintf("from the FCC records on March 3, 1993.\n\r\n\r");
  95.  
  96.     mprintf("Do you want the listing to pause after each 3 names? [Y/n] ==> ");
  97.     strin(buf,0);
  98.     if (toupper(buf[0]) != 'N')
  99.         pause++;
  100.  
  101.     mprintf("\n\r");
  102.     c[1] = '\0';
  103.     while (&counter)
  104.     {
  105.         memset(look,0,sizeof(look));
  106.         memset(buf,0,sizeof(buf));
  107.         mprintf("\n\r");
  108.  
  109.         mprintf("Enter C for Call search, N for Name search or Q to Quit:\n\r\n\r");
  110.         mprintf("Please choose C, N or Q ==> ");
  111.  
  112.         c[0] = toupper((char) getchar());
  113.         mprintf("%s\n\r",c);
  114.  
  115.         if (c[0] == 'Q')
  116.             quit();
  117.  
  118.         if (c[0] == '\n' || c[0] == '\r')
  119.             c[0] = 'C';
  120.  
  121.         if (c[0] != 'C' && c[0] != 'N')
  122.         {
  123.         mprintf("\n\rInvalid choice!  The options are C, N or Q.\n\r");
  124.             continue;
  125.         }
  126.         if (c[0] == 'N')
  127.         {
  128.             alphalook(cp,pause);
  129.             continue;
  130.         }
  131.         mprintf("\n\rEnter a Callsign or Q to Quit -> ");
  132.         strin(buf,1);
  133.         mprintf("\n\r");
  134.         sscanf(buf,"%s",look);
  135.  
  136.         if (!strlen(look))
  137.             continue;
  138.  
  139.         if (look[0] == 'Q')
  140.             continue;
  141.  
  142.         if ((int)strlen(look) > 7)
  143.         {
  144.     mprintf("Improper callsign format -> '%s'  Please try again\n\r",look);
  145.             continue;
  146.         }
  147.  
  148.         memset(call,0,sizeof(call));
  149.         strncpy(call,look,7);
  150.  
  151.         found = clook(cp,call,buf);
  152.  
  153.         if (found)
  154.         {
  155.             pcall(buf,mprintf);
  156.         }
  157.         else
  158.         mprintf("\n\r%s not found in callbook database.\n\r\n\r",call);
  159.     }
  160.     done();
  161. }
  162.  
  163. alphalook(cp,pause)
  164. FILE *cp;
  165. int pause;
  166. {
  167.     char sav[32];
  168.     char buf[32];
  169.     int recs;
  170.  
  171. mprintf("\n\rEnter a name or partial name, in the form of:  LAST FIRST MI\n\r");
  172.     mprintf("to search for.  Example: ==> Lloyd Fred L\n\r\n\r");
  173.     mprintf("Enter Name: ==> ");
  174.     strin(buf,0);
  175.  
  176.     if (!strlen(buf))
  177.         return;
  178.  
  179.     strcpy(sav,buf);
  180.     mprintf("\r\n\r\n");
  181.  
  182.     recs = namefind(cp,buf,pause,mprintf);
  183.  
  184.     if (recs)
  185.         mprintf("%d record(s) matched pattern '%s'.\n\r\n\r",
  186.             recs, sav);
  187.     else
  188.         mprintf("Pattern '%s' Not Found\n\r\n\r",sav);
  189. }
  190.  
  191. done()
  192. {
  193.     mprintf("\n\r\n\rYour usage limit has been reached.");
  194.     quit();
  195. }
  196.  
  197. quit()
  198. {
  199.     FILE *fp;
  200.     char str[256];
  201.     char when[32];
  202.     long now;
  203.  
  204. mprintf("\n\r\n\rThanks %s, for calling the AA7BQ Callsign Server.\n\r",        name);
  205. mprintf("Be sure and log in as 'BBS' sometime and check out the additional\n\r");
  206. mprintf("features available on this system.  73's  -fred AA7BQ\n\r");
  207.     time(&now);
  208.  
  209.     strftime(when,32,"%D %T",localtime(&now));
  210.     sprintf(str,"%s %s %d:%02d mins\n",when,name,
  211.         (now-start)/60,(now-start)%60);
  212.  
  213.     if (fp = fopen("call.log","a+"))
  214.         fputs(str,fp);
  215.     sleep(3);
  216.     restoremodes();
  217.     exit(0);
  218. }
  219.  
  220. strin(s,q)
  221. char *s;
  222. int q;
  223. {
  224.     char c;
  225.     char *ps = s;
  226.     int n = 0;
  227.  
  228.     while (1)
  229.     {
  230.         c = (char) getchar();
  231.         putchar(c);
  232.  
  233.         if (q && !n && (toupper(c) == 'Q'))
  234.             break;
  235.  
  236.         if ((c == '\n') || (c == '\r'))
  237.             break;
  238.  
  239.         if (isprint(c))
  240.         {
  241.             *s++ = toupper(c);
  242.             n++;
  243.         }
  244.     }
  245.     *s = '\0';
  246.     if (mon_handle)
  247.         write(mon_handle,ps,strlen(ps));
  248. }
  249.  
  250. struct termios ttysave;
  251.  
  252. setmodes()
  253. {
  254.     struct termios ttysnew;
  255.  
  256.     ioctl(0, TCGETS, &ttysave);
  257.     ttysnew = ttysave;
  258.     ttysnew.c_cflag &= ~CSIZE;
  259.     ttysnew.c_cflag |= CS8;
  260.     ttysnew.c_cflag &= ~PARENB;
  261.     ttysnew.c_oflag &= ~OLCUC;
  262.  
  263.     ttysnew.c_iflag = (IXANY | IGNPAR | IXON | ISTRIP);
  264.     ttysnew.c_lflag = ISIG;
  265.     ttysnew.c_cc[VINTR] = 0x0b;    /* Control K */
  266.     ttysnew.c_cc[VSUSP] = NULL;
  267.     ttysnew.c_cc[4] =1;
  268.     ttysnew.c_cc[5] =5;
  269.  
  270.     /* set new paramters */
  271.     ioctl(0, TCSETSF, &ttysnew);
  272. }
  273.  
  274.  
  275. /*
  276. **    restore original tty modes
  277. */
  278. restoremodes()
  279. {
  280.     ioctl(0, TCSETSF, &ttysave);
  281. }
  282.  
  283. monitor()
  284. {
  285.     FILE *fp;
  286.     char sbuf[256];
  287.     char montty[32];
  288.     struct termios newtty;
  289.  
  290.     struct stat st;
  291.  
  292.     mon_handle = 0;
  293.  
  294.     sprintf(sbuf,"/tmp/montty");
  295.     if (stat(sbuf,&st))
  296.         return(0);
  297.  
  298.     if ((fp = fopen(sbuf,"r")) == NULL)
  299.         return(0);
  300.  
  301.     fscanf(fp,"%s",montty);
  302.     fclose(fp);
  303.  
  304.     if (!strlen(montty))
  305.         return(0);
  306.  
  307.     if ((mon_handle = open(montty,
  308.         O_WRONLY|O_NDELAY|O_NOCTTY)) == -1)
  309.         return(0);
  310.     else
  311.     {
  312.         ioctl(mon_handle, TCGETS, &newtty);
  313.         newtty.c_lflag = ISIG;
  314.         ioctl(mon_handle, TCSETSF, &newtty);
  315.         tcflush(mon_handle,TCIOFLUSH);
  316.     }
  317.     return(mon_handle);
  318. }
  319.  
  320. mprintf(fmt,s1,s2,s3,s4,s5,s6,s7,s8)
  321. char *fmt;
  322. char *s1;
  323. char *s2;
  324. char *s3;
  325. char *s4;
  326. char *s5;
  327. char *s6;
  328. char *s7;
  329. char *s8;
  330. {
  331.     char pbuf[1024];
  332.  
  333.     printf(fmt,s1,s2,s3,s4,s5,s6,s7,s8);
  334.     if (mon_handle)
  335.     {
  336.         sprintf(pbuf,fmt,s1,s2,s3,s4,s5,s6,s7,s8);
  337.         write(mon_handle,pbuf,strlen(pbuf));
  338.     }
  339. }
  340.